using NUnit.Framework;
using Tycho.Lexer;
using Tycho.Parser.Peg;
using Tycho.Parser.Tokens;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class GrammarSpecification {
        [Test]
        public void ShouldReturnNullIfNotAllTokensAreParsed () {
            Grammar<object> g = new Grammar<object> (new NonTerminal<object> ("p", new SequenceRule<object> (new KeywordRule<object> ("x"), new KeywordRule<object> ("y"), new KeywordRule<object> ("z"))));

            Token[] tokens = NativeLexer.Lex ("x y z a").ToArray ();

            Assert.IsNull (g.Parse (tokens, null));
        }

        [Test]
        public void ShouldReturnNullIfTokensAreNotParsed () {
            Grammar<object> g = new Grammar<object> (new NonTerminal<object> ("p", new SequenceRule<object> (new KeywordRule<object> ("x"), new KeywordRule<object> ("y"), new KeywordRule<object> ("z"))));

            Token [] tokens = NativeLexer.Lex ("x y t").ToArray ();

            Assert.IsNull (g.Parse (tokens, null));
        }

        [Test]
        public void ShouldReturnTermIfTokensAreParsed () {
            Grammar<object> g = new Grammar<object> (new NonTerminal<object> ("p", new SequenceRule<object> (new KeywordRule<object> ("x"), new KeywordRule<object> ("y"), new KeywordRule<object> ("z"))));

            Token [] tokens = NativeLexer.Lex ("x y z").ToArray ();

            Term result = g.Parse (tokens, null);
            Assert.IsNotNull (result);
            Assert.IsNotNull (result is CompositeTerm);
            Assert.AreEqual ("p", ((CompositeTerm) result).Name);
        }
    }
}